var regionlevel;
var regions=[];
$(function(){
	/**
	 * 获取风琴菜单
	 */
	$.ajax({
         type: 'GET',
         dataType: "json",
         url: '/regionLevelRelation/getRegionLevel',
         success: function (data) {
             $.each(data, function (i, m) {
            	var ahtml = '<ul class="ulcls" id="'+m.levelid+'">';
    			 ahtml+='<li  class="selected"><a href="javascript:void(0);" class="roleusera" onclick="selectRegionLevel(\''+m.levelid+'\')">'+m.levelname+'</a></li>';
    			 ahtml+='</ul>';
				 $("#regionlevel").append(ahtml);
				 if(i==0){
					 regionlevel=m.levelid;
					 $("#"+m.levelid).addClass('select');
				 }
             });
           // getChecked();
          //  var regions=getRegion();
             getRegion();
         }
     });
})
/**
 * 获取地区列表
 * @returns {Array}
 */
function getRegion(){
	$.ajax({
		 type: 'GET',
         dataType: "json",
         data:{"levelid":regionlevel},
         url: '/regionLevelRelation/getRegions',
         success: function (data) {
        	 regions=data;
        	 setTree();
         }
	});
	return regions;
}
function setTree(){
	$("#region").tree({
		data:regions,
  		id:'id',
  		text:'text',
  		animate:true,
  		cascadeCheck:true,
  		attributes:'attributes',
  		checkbox:true
  	});
}
/**
 * 选择Region
 * @param levelid
 */
function selectRegionLevel(levelid){
	$("ul").removeClass("select");
	$("#"+levelid).addClass('select');
	regionlevel=levelid;
	getRegion();
}
/**
 * 保存
 */
function save(){
	var nodes=$("#region").tree('getChecked');
	var ids='';
	for(var i=0;i<nodes.length;i++){
		var target=nodes[i].domId;
		ids+=nodes[i].id+',';
	}
	var vo={};
	vo.levelid=regionlevel;
	vo.ids=ids;
	$.ajax({
		type:'post',
		url: '/regionLevelRelation/saveRelation',
		data:vo,
		success:function(data){
			if(data.code==200){
				$.messager.alert("成功","保存成功",'info',function(){
					getRegion();
				});
			}
			else{
				$.messager.alert("失败","保存失败："+data.msg,'error');
				getRegion();
			}
		},
		error:function(data){
			$.messager.alert("失败","保存失败：",'error');
			getRegion();
		}
	});
}


